package com.otod.pm.comment.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.comment.bo.AsmgCommentBo;
import com.otod.pm.comment.service.CommentService;
import com.otod.pm.comment.service.SubmitInfoService;
import com.otod.pm.comment.vo.AsmgCommentVo;
import com.otod.pm.comment.vo.AsmgSubmitInfoVo;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.doc.service.DocTpMasterService;
import com.otod.pm.doc.service.DocTpService;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.pm.prj.vo.TaskVo;
import com.otod.sys.amcm.MessageConstant;
import com.otod.sys.amcm.service.AmcmMsgAppService;
import com.otod.sys.emp.service.IEmpService;
import com.otod.sys.file.service.impl.FileServiceImpl;
import com.otod.sys.vo.FileVo;

@Controller
@RequestMapping("pm/comment")
public class CommentController extends BaseController {
	
	@Autowired
	private DocTpService docTpService;
	
	@Autowired
	private DocTpMasterService docTpMasterService;
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private SubmitInfoService submitInfoService;
	
	@Autowired
	private IEmpService empService;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private AmcmMsgAppService msgService;
	
	@Autowired
	private FileServiceImpl fileService;
	
	
	
	
	/**
	 * 保存文档模板
	 * @throws AppException 
	 */
	@RequestMapping(value = "save",method=RequestMethod.POST)
	@ResponseBody
	public String saveComment(AsmgCommentVo commentVo,HttpServletRequest request) throws AppException{
		super.setInsertOperInfo(commentVo);
		commentService.insert(commentVo);
		return JsonSuccess.factory("评论成功！");
	}
	
	
	
	
	
	/**
	 * 根据id获取评论信息
	 * @param prjType
	 * @return
	 */
	@RequestMapping(value="getCommentById",method=RequestMethod.POST)
	@ResponseBody
	public AsmgCommentVo getCommentById(@RequestBody String id,HttpServletRequest request) {
		
		return commentService.getById(id);
   }
	
	/**
	 * 查询当前评论对象的所有相关评论
	 * @return 
	 */
	@RequestMapping(value = "getAllAboutComment", method = RequestMethod.POST)
	@ResponseBody
	public List<AsmgCommentBo> getAllAboutComment(String busitableKey) {
		return commentService.queryChildrenForList(busitableKey);
	}

	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String deleteById(@RequestBody String id) throws AppException {
		commentService.deleteById(id);
		
		return JsonSuccess.factory("删除评论成功！");
	}
	
	/**
	 * 提交审核
	 * @throws AppException 
	 */
	@RequestMapping(value = "submitAudit",method = RequestMethod.GET)
	@ResponseBody
	public String submitAudit(HttpServletRequest request) throws AppException {
		//commentService.deleteById(id);
		Map params = super.getParamMapFromRequest(request);
		
		SessionUserHolder user = SessionUserUtil.getCurrentUser();
		String userNo = user.getUserNo();
		
		AsmgSubmitInfoVo submitInfoVo = new AsmgSubmitInfoVo();
		submitInfoVo.setSubmitEmpNo(userNo);
		submitInfoVo.setSubmitType(PmConstant.FRUIT);
		submitInfoVo.setStatus(PmConstant.SUBMIT);
		
		String prjId = (String) params.get("prjId");
		String busitableKey = (String)params.get("fileId");
		if(StringUtils.equals(prjId, busitableKey)){
			
			submitInfoVo.setSubmitType(PmConstant.PROJECT);
			//說明是項目提交審核
		}
		
		submitInfoVo.setBusitableKey(busitableKey);
		submitInfoVo.setPrjId(prjId);
		super.setInsertOperInfo(submitInfoVo);
		
		String submitEmpNo = (String)params.get("submitEmpNo");
		
		Map<String,Object> map = new HashMap<String,Object>();
		super.setUpdateOperInfo(map);
		
		submitInfoService.submit(submitInfoVo,submitEmpNo,map);
		return JsonSuccess.factory("提交审核成功！");
	}
	
	/**
	 * 审核通过/不通过
	 * @throws AppException 
	 */
	@RequestMapping(value = "audit",method = RequestMethod.GET)
	@ResponseBody
	public String audit(HttpServletRequest request) throws AppException {
		Map params = super.getParamMapFromRequest(request);
		
		SessionUserHolder user = SessionUserUtil.getCurrentUser();
		String userNo = user.getUserNo();
		
		AsmgSubmitInfoVo submitInfoVo = new AsmgSubmitInfoVo();
		
		String busitableKey = (String) params.get("prjId");
		String isAgree = (String)params.get("agree");
		Map<String,Object> map  = new HashMap<String,Object>();
		map.put("busitableKey", busitableKey);
		map.put("targetEmpNo", userNo);
		List<AsmgSubmitInfoVo> list = submitInfoService.queryForList(map);
		if(!Collections3.isEmpty(list)){
			submitInfoVo = list.get(0);
		}else{
			throw new AppException("未提交审核或无审核权限！");
		}
		if(PmConstant.SUCCESS.equals(submitInfoVo.getStatus())){
			throw new AppException("审核已通过，无需再次审核");
		}else if(PmConstant.FAILURE.equals(submitInfoVo.getStatus())){
			throw new AppException("审核已经不通过，无需再次审核！");
		}
		map.clear();
		map.put("uuid", submitInfoVo.getUuid());
		map.put("comment", (String)params.get("comment"));
		if("agree".equals(isAgree)){
			map.put("status", PmConstant.SUCCESS);
			submitInfoVo.setShowName(MessageConstant.AUDIT_SUCCESS);
		}else if("disAgree".equals(isAgree)){
			map.put("status", PmConstant.FAILURE);
			submitInfoVo.setShowName(MessageConstant.AUDIT_FRILURE);
			// 审核不通过 则改变项目的状态回准备中
			Map<String,Object> projectMap = new HashMap<String,Object>();
			projectMap.put("uuid", busitableKey);
			projectMap.put("status", PmConstant.PRJ_STATUS_WAITING);
			super.setUpdateOperInfo(projectMap);
			projectService.update(projectMap);
		}
		super.setUpdateOperInfo(map);
		
		submitInfoService.updateTp(map);
		
		sendMessage(submitInfoVo);
		return JsonSuccess.factory("谢谢评价！");
		
		
		
	}
	
	/**
	 * 项目查询方法
	 * @param request
	 * @return
	 */
	
	@RequestMapping(value="querySubmitInfo",method=RequestMethod.GET)
	@ResponseBody
	public Page querySubmitInfo(HttpServletRequest request,String prj_name){
		Map<String,Object> params = super.getParamMapFromRequest(request);
		if(StringUtils.isNotEmpty(prj_name)){
			params.put("status", prj_name);
		}
		SessionUserHolder user = SessionUserUtil.getCurrentUser();
		String empNo = user.getUserNo();
		
		params.put("targetEmpNo", empNo);
		
		Page page = new Page(request);
		return submitInfoService.queryForList(page,params);
	}
	
	/**
	 * 查询项目审核信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="showComments",method=RequestMethod.POST)
	@ResponseBody
	public List<AsmgSubmitInfoVo> showComments(@RequestBody String prjId){
		Map<String,Object> map  = new HashMap<String,Object>();
		map.put("busitableKey", prjId);
		List<AsmgSubmitInfoVo> list  =submitInfoService.queryForList(map);
		for (AsmgSubmitInfoVo asmgSubmitInfoVo : list) {
			if(PmConstant.SUCCESS.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("审核成功");
			}else if(PmConstant.FAILURE.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("审核失败");
			}else if(PmConstant.SUBMIT.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("未审核");
			}
			
			if(StringUtils.isEmpty(asmgSubmitInfoVo.getUpdateEmp())){
				asmgSubmitInfoVo.setUpdateEmp(empService.findByEmpNo(asmgSubmitInfoVo.getTargetEmpNo()).getEmpName());
			}
		}
		return list;
	}
	
	public void sendMessage(AsmgSubmitInfoVo submitInfoVo){
		TaskVo vo = new TaskVo();
		vo.setPrjId(submitInfoVo.getPrjId());
		vo.setUuid(submitInfoVo.getBusitableKey());
		vo.setEmpNo(submitInfoVo.getCreateEmpNo());
		vo.setEmpName(submitInfoVo.getCreateEmp());
		if(PmConstant.FRUIT.equals(submitInfoVo.getSubmitType())){
			
			FileVo fileVo = fileService.getById(submitInfoVo.getBusitableKey());
			vo.setTaskName(fileVo.getFileName());
		}else if(PmConstant.PROJECT.equals(submitInfoVo.getSubmitType())){
			
			ProjectVo projectrVo = projectService.getById(submitInfoVo.getBusitableKey());
			vo.setTaskName(projectrVo.getPrjName());
		}
		msgService.installMsgVo(vo, submitInfoVo.getShowName());
	}
}
